/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.mess.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.bus.entity.Entinfo;
import com.thinkgem.jeesite.modules.gen.entity.GenTable;
import com.thinkgem.jeesite.modules.sys.entity.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.mess.entity.MessHall;
import com.thinkgem.jeesite.modules.mess.service.MessHallService;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 菜谱管理Controller
 * @author dby
 * @version 2020-08-20
 */
@Controller
@RequestMapping(value = "${adminPath}/mess/messHall")
public class MessHallController extends BaseController {

	@Autowired
	private MessHallService messHallService;
	
	@ModelAttribute
	public MessHall get(@RequestParam(required=false) String id) {
		MessHall entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = messHallService.get(id);
		}
		if (entity == null){
			entity = new MessHall();
		}
		return entity;
	}

	/*
	* 获取订餐类别
	* */
	public Map<String,String> getCategoryMap(){

		Map<String,String> map = new HashMap<>();
		map.put("早餐","breakfast");
		map.put("午餐","lunch");
		map.put("晚餐","dinner");
		map.put("课间水果","fruit");
		map.put("加餐","snock");
		return map;
	}
	@RequiresPermissions("mess:messHall:view")
	@RequestMapping(value = {"list", ""})
	public String list(MessHall messHall, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MessHall> page = messHallService.findPage(new Page<MessHall>(request, response), messHall); 
		model.addAttribute("page", page);
		//获取当前周 时间集合
		List<Date> dateList = DateUtils.getWeekDateList() ;
//		MessHall m = new MessHall();
//		//传入一周时间 数组
		messHall.setDateList(dateList);
//		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
//		List<Map<String,String>> dates=new ArrayList<>();
//		for(int i=1;i<=dateList.size();i++){
//			Date date=dateList.get(i-1);
//			Map<String,String> map=new HashMap<>();
//			map.put("date",simpleDateFormat.format(date));
//			String name="";
//			switch (i){
//				case 1:name="星期一";
//					break;
//				case 2:name="星期二";
//					break;
//				case 3:name="星期三";
//					break;
//				case 4:name="星期四";
//					break;
//				case 5:name="星期五";
//					break;
//				case 6:name="星期六";
//					break;
//				case 7:name="星期日";
//			}
//			map.put("name",name);
//			dates.add(map);
//		}
//		m.setDates(dates);
//		//传入 订餐类别map
//		m.setCategoryMap(getCategoryMap());
//
//		List<Map<String, Object>> messHallList = messHallService.findMessTableList(m);
//
//		model.addAttribute("messHallList", messHallList);
//		model.addAttribute("map", m.getCategoryMap());




		List<List> messHallList = messHallService.findHallList(messHall);
		model.addAttribute("messHallList" ,messHallList);



		//处理开始结束时间 在页面显示
		model.addAttribute("beginDate", DateUtils.formatDate(dateList.get(0),"yyyy年MM月dd"));
		model.addAttribute("endDate", DateUtils.formatDate(dateList.get(6),"yyyy日MM月dd"));

		return "modules/mess/messHallList";
	}

	@RequiresPermissions("mess:messHall:view")
	@RequestMapping(value = "form")
	public String form(MessHall messHall, Model model) {
		model.addAttribute("messHall", messHall);

//		List<MessHall> columnList = new ArrayList<MessHall>();
//		model.addAttribute("columnList", columnList);

		List<Date> list = DateUtils.getWeekDateList() ;
		MessHall m = new MessHall();
		m.setDateList(list);

		// 获取物理表列表
		List<MessHall> messHallList = messHallService.findTableList(m);
		model.addAttribute("messHallList", messHallList);


		return "modules/mess/messHallForm";
	}

	@RequiresPermissions("mess:messHall:edit")
	@RequestMapping(value = "save")
	public String save(MessHall messHall, Model model, RedirectAttributes redirectAttributes) {
//		if (!beanValidator(model, messHall)){
//			return form(messHall, model);
//		}
		List<MessHall> messHallList = messHall.getMessHallList();
		for(MessHall messHall1:messHallList ){
			messHallService.save(messHall1);
		}
		addMessage(redirectAttributes, "保存添加菜谱成功");
		return "redirect:"+Global.getAdminPath()+"/mess/messHall/?repage";
	}
	
	@RequiresPermissions("mess:messHall:edit")
	@RequestMapping(value = "delete")
	public String delete(MessHall messHall, RedirectAttributes redirectAttributes) {
		messHallService.delete(messHall);
		addMessage(redirectAttributes, "删除添加菜谱成功");
		return "redirect:"+Global.getAdminPath()+"/mess/messHall/?repage";
	}



	@RequiresPermissions("mess:messHall:view")
	@RequestMapping(value = "index")
	public String index(MessHall messHall, Model model) {
		model.addAttribute("messHall", messHall);

		//获取当前周 时间集合
		List<Date> WeekDateList = DateUtils.getWeekDateList() ;
		MessHall mh = new MessHall();
		//传入一周时间 数组
		mh.setDateList(WeekDateList);

		// 获取物理表列表
		List<MessHall> messHallList = messHallService.findWeekList(mh);
		model.addAttribute("messHallList", messHallList);

		return "modules/mess/messHallIndex";
	}

}